<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <meta name="description" content="Kitchen Sink | Cypress Example">
  <meta name="author" content="Cypress.io">
  <meta name="copyright" content="Cypress.io, Inc">

  <title>Cypress.io: Kitchen Sink</title>

  <link rel="icon" href="/assets/img/favicon.ico" type="image/x-icon">
  <link rel="stylesheet" href="/assets/css/vendor/bootstrap.min.css">
  <link rel="stylesheet" href="/assets/css/vendor/fira.css">
  <link rel="stylesheet" href="/assets/css/styles.css">
</head>

<body>
  <nav class="navbar navbar-inverse">
    <div class="container">
      <div class="navbar-header">
        <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#navbar">
          <span class="icon-bar"></span>
          <span class="icon-bar"></span>
          <span class="icon-bar"></span>
        </button>
        <a class="navbar-brand" href="/">cypress.io</a>
      </div>
      <div id="navbar" class="navbar-collapse collapse">
        <ul class="nav navbar-nav">
          <li class="active" class="dropdown">
            <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button">Commands <span class="caret"></span></a>
            <ul class="dropdown-menu">
              <li><a href="/commands/querying">Querying</a></li>
              <li><a href="/commands/traversal">Traversal</a></li>
              <li><a href="/commands/actions">Actions</a></li>
              <li><a href="/commands/window">Window</a></li>
              <li><a href="/commands/viewport">Viewport</a></li>
              <li><a href="/commands/location">Location</a></li>
              <li><a href="/commands/navigation">Navigation</a></li>
              <li><a href="/commands/assertions">Assertions</a></li>
              <li><a href="/commands/misc">Misc</a></li>
              <li><a href="/commands/connectors">Connectors</a></li>
              <li><a href="/commands/aliasing">Aliasing</a></li>
              <li><a href="/commands/waiting">Waiting</a></li>
              <li><a href="/commands/network-requests">Network Requests</a></li>
              <li class="active"><a href="/commands/files">Files</a></li>
              <li><a href="/commands/local-storage">Local Storage</a></li>
              <li><a href="/commands/cookies">Cookies</a></li>
              <li><a href="/commands/spies-stubs-clocks">Spies, Stubs &amp; Clocks</a></li>
            </ul>
          </li>
          <li><a href="/utilities">Utilities</a></li>
          <li><a href="/cypress-api">Cypress API</a></li>
        </ul>
        <ul class="nav navbar-nav pull-right">
          <li><a href="https://github.com/cypress-io/cypress-example-kitchensink">GitHub</a></li>
        </ul>
      </div>
    </div>
  </nav>

  <div class="banner">
    <div class="container">
      <h1>Files</h1>
      <p>Examples of using files to represent data, read data or write data in Cypress, for a full reference of commands, go to <a href="https://on.cypress.io/api" target="_blank">docs.cypress.io</a>
      </p>
    </div>
  </div>
  <div class="container content-container">
    <div id="fixtures">
      <div class="row">

        <div class="col-xs-7">
          <h4 id="fixture"><a href="https://on.cypress.io/fixture">cy.fixture()</a></h4>
          <p>To load a fixture, use the <a href="https://on.cypress.io/fixture"><code>cy.fixture()</code></a> command.</p>
          <pre><code class="javascript">// Instead of writing a response inline you can
// use a fixture file's content.

cy.server()
cy.fixture('example.json').as('comment')
cy.route('GET', 'comments', '@comment').as('getComment')

// we have code that gets a comment when
// the button is clicked in scripts.js
cy.get('.fixture-btn').click()

cy.wait('@getComment').its('responseBody')
  .should('have.property', 'name')
  .and('include', 'Using fixtures to represent data')

// you can also just write the fixture in the route
cy.route('GET', 'comments', 'fixture:example.json').as('getComment')

// we have code that gets a comment when
// the button is clicked in scripts.js
cy.get('.fixture-btn').click()

cy.wait('@getComment').its('responseBody')
  .should('have.property', 'name')
  .and('include', 'Using fixtures to represent data')

// or write fx to represent fixture
// by default it assumes it's .json
cy.route('GET', 'comments', 'fx:example').as('getComment')

// we have code that gets a comment when
// the button is clicked in scripts.js
cy.get('.fixture-btn').click()

cy.wait('@getComment').its('responseBody')
  .should('have.property', 'name')
  .and('include', 'Using fixtures to represent data')</code></pre>
        </div>
        <div class="col-xs-5">
          <div class="well">
            <button class="fixture-btn btn btn-primary">Get Comment</button>
            <div class="fixture-comment"></div>
          </div>
        </div>

        <div class="col-xs-12"><hr></div>
      </div>

      <div class="row">

        <div class="col-xs-7">
          <h4 id="require">cy.fixture or require</h4>
          <p>You can use <code>require</code> to load JSON fixtures.</p>
          <pre><code class="javascript">/// JSON fixture file can be loaded directly using
// the built-in JavaScript bundler
const requiredExample = require('../../fixtures/example')

beforeEach(() => {
  // load example.json fixture file and store
  // in the test context object
  cy.fixture('example.json').as('example')
})

it('cy.fixture or require', function () {
  // we are inside the "function () { ... }"
  // callback and can use test context object "this"
  // "this.example" was loaded in "beforeEach" function callback
  expect(this.example, 'fixture in the test context')
    .to.deep.equal(requiredExample)

  // or use "cy.wrap" and "should('deep.equal', ...)" assertion
  cy.wrap(this.example, 'fixture vs require')
    .should('deep.equal', requiredExample)
})</code></pre>
        </div>

        <div class="col-xs-12"><hr></div>
      </div>
    </div>


    <div id="readfile">
      <div class="row">

        <div class="col-xs-12">
          <h4 id="readFile"><a href="https://on.cypress.io/readfile">cy.readFile()</a></h4>
          <p>To read a file's content, use the <a href="https://on.cypress.io/readfile"><code>cy.readFile()</code></a> command.</p>
          <pre><code class="javascript">// You can read a file and yield its contents
// The filePath is relative to your project's root.
cy.readFile('cypress.json').then((json) => {
  expect(json).to.be.an('object')
})</code></pre>
        </div>

        <div class="col-xs-12"><hr></div>
      </div>
    </div>

    <div id="writefile">
      <div class="row">

        <div class="col-xs-12">
          <h4 id="writefile"><a href="https://on.cypress.io/writefile">cy.writeFile()</a></h4>
          <p>To write to a file with the specified contents, use the <a href="https://on.cypress.io/writefile"><code>cy.writeFile()</code></a> command.</p>
          <pre><code class="javascript">// You can write to a file

// Use a response from a request to automatically
// generate a fixture file for use later
cy.request('https://jsonplaceholder.cypress.io/users')
  .then((response) => {
    cy.writeFile('cypress/fixtures/users.json', response.body)
  })
cy.fixture('users').should((users) => {
  expect(users[0].name).to.exist
})

// JavaScript arrays and objects are stringified
// and formatted into text.
cy.writeFile('cypress/fixtures/profile.json', {
  id: 8739,
  name: 'Jane',
  email: 'jane@example.com',
})

cy.fixture('profile').should((profile) => {
  expect(profile.name).to.eq('Jane')
})</code></pre>
        </div>

        <div class="col-xs-12"><hr></div>
      </div>
    </div>
  </div>

  <script src="/assets/js/vendor/jquery-1.12.0.min.js"></script>
  <script src="/assets/js/vendor/bootstrap.min.js"></script>
  <script src="/assets/js/vendor/highlight.pack.js"></script>
  <script src="/assets/js/scripts.js"></script>
</body>
</html>
